package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.company;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.CompanyExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CAshGateVideoBindServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.CompanyListVo;
import com.xiaoxu.intranetweb.util.IllegalCharacterCheck;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.Future;

/**
 * 客户单位业务层
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2023/2/07 15:00
 * @since 1.0
 */
@Component
@Slf4j
public class CompanyBu {
    @Autowired
    private ICCompanysService companysService;
    @Autowired
    private ISysUserOrgConnService sysUserOrgConnService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ITPlantsService plantsService;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private ICCompanyClassService companyClassService;
    @Autowired
    private ICFinaceEnterService finaceEnterService;
    @Autowired
    private ICIcCardsInfoService icCardsInfoService;
    @Autowired
    private ICOrderDetailService orderDetailService;
    @Autowired
    private ICPdtsAuthService pdtsAuthService;
    @Autowired
    private ICWeightSheetsService weightSheetsService;
    @Autowired
    private CAshGateVideoBindServiceImpl ashGateVideoBindService;
    @Autowired
    private ICTrucksService iCTrucksService;
    //    查询所有公司

    /**
     * 根据单位类型返回所有该类型的单位
     *
     * @param value     单位名称
     * @param className 单位类型
     * @return
     */
    public SzResult getCompany(String value, String className, String phone, String cpyClassClass,String plantId) {
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like(value != null && !value.isEmpty(), "CPY_CO", value);
//        在邹县将CPY_ADDR改成了单位类型（手填）
        queryWrapper.and(value != null && !value.isEmpty(),i ->i.like("CPY_CO",value).or().like("CPY_ADDR",value));
        queryWrapper.like(className != null && !className.isEmpty(), "CPY_CO_CLASS", className);
        queryWrapper.like(cpyClassClass != null && !cpyClassClass.isEmpty(), "CPY_CO_CLASS", cpyClassClass);
        queryWrapper.eq(phone != null && !phone.isEmpty(), "CPY_LINK_MODE", phone);
        queryWrapper.eq("CPY_DELETED", 0);
        queryWrapper.eq("DATA_FROM",plantId);
        //根据CPY_PASS和CPY_TEMP_LOCK字段排序，冻结的单位放后面
        queryWrapper.orderByAsc("CPY_PASS");
        queryWrapper.orderByAsc("CPY_TEMP_LOCK");
        queryWrapper.orderByDesc("CPY_CO");//降序
        queryWrapper.orderByDesc("CPY_ADDR");//降序
        List<CompanyListVo> list = new ArrayList<>();

        for (CCompanys cCompanys : companysService.list(queryWrapper)) {
            CompanyListVo companyListVo = new CompanyListVo();
            BeanUtils.copyProperties(cCompanys, companyListVo);
          if (companyListVo.getLoadSite()!=null){
              QueryWrapper<CAshGateVideoBind> qw = new QueryWrapper<>();
              qw.in("ID",cCompanys.getLoadSite().split(",")).select("ID", "ASHGATE_NAME","ASHGATE_SHRTNAME");
              List<CAshGateVideoBind> cAshGateVideoBinds = ashGateVideoBindService.list(qw);
              companyListVo.setLoadSiteList(cAshGateVideoBinds);
          }
            list.add(companyListVo);
        }
        return SzResult.success(list);
    }

    //新增单位
    public SzResult addCompany(String cpyCo, String cpyChiCo, String cpyShrtname, String cpyCoClass, String cpyAddr, String cpyPass, String cpyTempLock, String cpyLongitude, String cpyLatitude, String cpyLinkMan, String cpyLinkMode,String beizhu, String token,String plantId) throws Exception {
//      校验非法字符
       if (IllegalCharacterCheck.isNameLawful(cpyCo)){
           return SzResult.error("单位名里有非法字符");
       }
       if (IllegalCharacterCheck.isNameLawful(cpyChiCo)){
           return SzResult.error("子单位名里有非法字符");
       }
       if (IllegalCharacterCheck.isNameLawful(cpyShrtname)){
           return SzResult.error("单位简称里有非法字符");
       }
        String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
        CCompanys companys = new CCompanys();
        companys.setCpyPass(0);
        companys.setCpyTempLock(0);
        companys.setCpyRemark(beizhu);
        //判断ccompany表中是否有该单位
        QueryWrapper<CCompanys> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("CPY_CO", cpyCo);
        queryWrapper2.eq("CPY_CHICO", cpyChiCo);
        queryWrapper2.eq("CPY_DELETED", 0);
        CCompanys cCompanys = companysService.getOne(queryWrapper2);
        if (cCompanys != null) {
            return SzResult.error("该单位已存在");
        }
        if (cpyCo != null && !cpyCo.equals("")) {
            //屏蔽字母和数字和特殊字符
//            if (cpyCo.matches(".*[a-zA-Z0-9].*")) {
//                return SzResult.error("单位名称不能包含字母和数字");
//            }
            //不能包含特殊字符
            // 正则表达式匹配除了中文和英文括号之外的特殊字符
            // 使用 ^ 符号在字符类内部表示除了指定字符之外的所有字符
//            String regex = "[^\\w\\s（）()]+";
//            Pattern pattern = Pattern.compile(regex);
//
//            Matcher matcher = pattern.matcher(cpyCo);
//            if (matcher.find()) {
//                return SzResult.error("单位名称不能包含特殊字符");
//            }
            companys.setCpyCo(cpyCo);
        }
        if (cpyChiCo != null && !cpyChiCo.equals("")) {
            companys.setCpyChico(cpyChiCo);
        }
        //单位简称
        if (cpyShrtname != null && !cpyShrtname.equals("")) {
            //去重
            QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("CPY_SHRTNAME", cpyShrtname);
            queryWrapper.eq("CPY_DELETED", 0);
            CCompanys cCompanys1 = companysService.getOne(queryWrapper);
            if (cCompanys1 != null) {
                return SzResult.error("该单位简称已存在");
            }
            companys.setCpyShrtname(cpyShrtname);
        }
        //单位类型
        if (cpyCoClass != null && !cpyCoClass.equals("")) {
            companys.setCpyCoClass(cpyCoClass);
        }
        //单位地址
        if (cpyAddr != null && !cpyAddr.equals("")) {
            companys.setCpyAddr(cpyAddr);
        }
        if (cpyLongitude != null && !cpyLongitude.equals("")) {
            companys.setCpyLongitude(Double.valueOf(cpyLongitude));
        }
        if (cpyLatitude != null && !cpyLatitude.equals("")) {
            companys.setCpyLatitude(Double.valueOf(cpyLatitude));
        }
        if (cpyLinkMan != null && !cpyLinkMan.equals("")) {
            companys.setCpyLinkman(cpyLinkMan);
        }
        if (cpyLinkMode != null && !cpyLinkMode.equals("")) {
            companys.setCpyLinkMode(cpyLinkMode);
        }
        companys.setCpyDeleted(0);
            companys.setId(uuid);
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("CPY_ID");
        queryWrapper.last("limit 1");
        queryWrapper.eq("DATA_FROM", plantId);
        CCompanys companys1 = companysService.getOne(queryWrapper);
        if (companys1 != null) {
            BigDecimal cpyId = companys1.getCpyId();
            String cpyId1 = String.valueOf((cpyId.intValue() + 1));
            companys.setCpyId(BigDecimal.valueOf(Long.parseLong(cpyId1)));
        } else {
            companys.setCpyId(BigDecimal.valueOf(1));
// cAshGateVideoBind.setAshgateId(uuid);
        }
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            companys.setDataFrom(plantId);
            companys.setDownedID1(0);
            companys.setCpyPass(0);
            companys.setCpyTempLock(0);
            companysService.save(companys);
            sysLogBu.logRecord("新增单位信息", user.getTrueName(), "新增了【" + companys.getCpyCo() +"."+companys.getCpyChico()+ "】的单位信息", plantId);
            return SzResult.Success();
    }

    //删除单位
    public SzResult deleteCompany(String id,String cpyId, String token,String plantId) throws Exception {
       // String plantId = plantId;
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("CPY_DELETED", 0);
        CCompanys companys = companysService.getOne(queryWrapper);
        companys.setCpyDeleted(1);
        companys.setDownedID1(0);
//看该单位有没有绑定ic卡
        QueryWrapper<CIcCardsInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1
                .and(wrapper -> wrapper.eq("CARDINF_CO", companys.getCpyCo()).eq("CARDINF_CHI_CO", companys.getCpyChico()))
                .or(wrapper -> wrapper.eq("CARDINF_CO_SUPPLY", companys.getCpyCo()).eq("CARDINF_CHICO_SUPPLY", companys.getCpyChico()))
                .or(wrapper -> wrapper.eq("CARDINF_CO_TRAN", companys.getCpyCo()).eq("CARDINF_CHICO_TRAN", companys.getCpyChico()))
                .eq("CARDINF_DELETED", 0)
                .eq("DATA_FROM", plantId)
                .and(wrapper -> wrapper.eq("CARDINF_MODIFY_REASON", "发卡").or().eq("CARDINF_MODIFY_REASON", "解挂"))
                .last("LIMIT 1");
        CIcCardsInfo icCardsInfo = icCardsInfoService.getOne(queryWrapper1);
        if (icCardsInfo != null) {
            return SzResult.error("该单位已绑定ic卡，不能删除");
        }

        //看该单位有没有产生磅单
        QueryWrapper<CWeightSheets> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(CWeightSheets::getWeishtCo, companys.getCpyCo()).eq(CWeightSheets::getWeishtChiCo, companys.getCpyChico()).
                or(i -> i.eq(CWeightSheets::getWeishtCoSupply, companys.getCpyCo()).eq(CWeightSheets::getWeishtCoSupplyChi, companys.getCpyChico())).
                or(i -> i.eq(CWeightSheets::getWeishtCoTran, companys.getCpyCo()).eq(CWeightSheets::getWeishtChiCoTran, companys.getCpyChico()));
        queryWrapper2.eq("WEISHT_DELETED", 0);
        queryWrapper2.eq("DATA_FROM", plantId);
        queryWrapper2.last("limit 1");
        CWeightSheets weightSheets = weightSheetsService.getOne(queryWrapper2);
        if (weightSheets != null) {
            return SzResult.error("该单位已产生磅单，不能删除");
        }
        companysService.updateById(companys);
        return SzResult.Success();
    }

    //修改单位
    public SzResult updateCompany(String id,String cpyId, String cpyCo, String cpyChico, String cpyShrtname, String cpyCoClass, String cpyAddr, String cpyPass, String cpyTempLock, String cpyLongitude, String cpyLatitude, String cpyLinkMan, String cpyLinkMode,String beizhu, String token,String plantId) throws Exception {
        if (IllegalCharacterCheck.isNameLawful(cpyCo)){
            return SzResult.error("单位名里有非法字符");
        }
        if (IllegalCharacterCheck.isNameLawful(cpyChico)){
            return SzResult.error("子单位名里有非法字符");
        }
        if (IllegalCharacterCheck.isNameLawful(cpyShrtname)){
            return SzResult.error("单位简称里有非法字符");
        }
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("CPY_DELETED", 0);
        CCompanys companys = companysService.getOne(queryWrapper);
        String originalCpyCo = companys.getCpyCo();
        String originalCpyChiCo = companys.getCpyChico();
        if (cpyCo != null && !cpyCo.equals("")) {
            companys.setCpyCo(cpyCo);
            coCorrelationTable(cpyCo, originalCpyCo, originalCpyChiCo,plantId);
        }else{
            return SzResult.error("单位名称不能为空");
        }
        if (cpyChico != null && !cpyChico.equals("")) {
            companys.setCpyChico(cpyChico);
            chicoCorrelationTable(cpyChico, originalCpyCo, originalCpyChiCo,plantId);
        }else{
            return SzResult.error("子单位名称不能为空");
        }
        if (beizhu != null && !beizhu.equals("")) {
            companys.setCpyRemark(beizhu);
        }else{
            companys.setCpyRemark("");
        }
        if (cpyShrtname != null && !cpyShrtname.equals("")) {
            companys.setCpyShrtname(cpyShrtname);
        }else {
            companys.setCpyShrtname("");
        }
        if (cpyCoClass != null && !cpyCoClass.equals("")) {
            companys.setCpyCoClass(cpyCoClass);
        }else {
            companys.setCpyCoClass("");
        }
        if (cpyAddr != null && !cpyAddr.equals("")) {
            companys.setCpyAddr(cpyAddr);
        }else {
            companys.setCpyAddr("");
        }
        if (cpyPass != null && !cpyPass.equals("")) {
            companys.setCpyPass(Integer.valueOf(cpyPass));
        }else {
            companys.setCpyPass(0);
        }
        if (cpyTempLock != null && !cpyTempLock.equals("")) {
            companys.setCpyTempLock(Integer.valueOf(cpyTempLock));
        }else {
            companys.setCpyTempLock(0);
        }
        if (cpyLongitude != null && !cpyLongitude.equals("")) {
            companys.setCpyLongitude(Double.valueOf(cpyLongitude));
        }else {
            companys.setCpyLongitude(0.0);
        }
        if (cpyLatitude != null && !cpyLatitude.equals("")) {
            companys.setCpyLatitude(Double.valueOf(cpyLatitude));
        }else {
            companys.setCpyLatitude(0.0);
        }

        if (cpyLinkMan != null && !cpyLinkMan.equals("")) {
            companys.setCpyLinkman(cpyLinkMan);
        }else {
            companys.setCpyLinkman("");
        }
        if (cpyLinkMode != null && !cpyLinkMode.equals("")) {
            companys.setCpyLinkMode(cpyLinkMode);
        }else {
            companys.setCpyLinkMode("");
        }
        companys.setDownedID1(0);
        companysService.updateById(companys);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("修改单位信息", user.getTrueName(), "修改了【" + companys.getCpyCo() + "】的单位信息",plantId);
        return SzResult.Success();
    }

    //修改单位绑定装载点
    public SzResult updateLoadSite(String id,String loadSite, String token,String plantId) throws Exception {
        if (StringUtils.isNotEmpty(loadSite)) {
            CCompanys companys1 = companysService.getById(id);
            CCompanys companys = new CCompanys();
            companys.setId(id);
            companys.setLoadSite(loadSite);
            companysService.updateById(companys);
            List<String> loadSiteList = Arrays.asList(loadSite.split(","));
            List<CAshGateVideoBind> list = ashGateVideoBindService.list(new QueryWrapper<CAshGateVideoBind>().in("ID", loadSiteList));
            List<String> videoList = new ArrayList<>();
            for (CAshGateVideoBind ashGateVideoBind : list) {
                videoList.add(ashGateVideoBind.getAshgateName());
            }
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("修改单位信息", user.getTrueName(), "修改了【" + companys1.getCpyCo()+"."+companys1.getCpyChico() + "】单位的绑定装载点信息【"+ videoList +"】",plantId);
            return SzResult.Success();
        }else {
            CCompanys companys1 = companysService.getById(id);
            CCompanys companys = new CCompanys();
            companys.setId(id);
            companys.setLoadSite("");
            companysService.updateById(companys);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("修改单位信息", user.getTrueName(), "修改了【" + companys1.getCpyCo()+"."+companys1.getCpyChico() + "】单位的绑定装载点信息【】",plantId);
            return SzResult.Success();
        }
    }


    /**
     * 导出excel
     *
     * @param fields    传入的字段
     * @param filename  文件名
     * @param ids       传入的id
     * @param isHeader  是否有表头
     * @param mode      导出模式
     * @param original  是否原始数据
     * @param pager     分页
     * @param sheetName sheet名
     * @return 返回一个Future
     */
    @Async
    public Future<List<CompanyExcelEn>> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader,
                                                    String mode, Boolean original, Map<String, String> pager, String sheetName, String token, Map<String, String> form,String plantId) throws Exception {
        //通过token获取plantId
//        QueryWrapper<SysUserPlantConn> queryWrapper4 = new QueryWrapper<>();
//        queryWrapper4.eq("USER_ID", StpUtil.getLoginIdByToken(token).toString());
//        SysUserPlantConn one2 = userPlantConnService.getOne(queryWrapper4);
       // String plantId = one2.getPlantId();
        List<Map<String, Object>> newIds = new ArrayList<>();
        List<CompanyExcelEn> CompanyExcelEns = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    //导出当前页
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            Object object = field.get("field");//获得的是field对应的值
                            if (id.containsKey(object)) {
                                newId.put(object.toString(), id.get(object));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> stringObjectMap : newIds) {
                        CompanyExcelEn companyExcelEn = CompanyExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
                            if (entry.getKey().equals("cpyCo")) {
                                companyExcelEn.setCpyCo(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyShrtname")) {
                                companyExcelEn.setCpyShrtName(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyCoClass")) {
                                companyExcelEn.setCpyCoClass(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyAddr")) {
                                companyExcelEn.setCpyAddr(entry.getValue().toString());
                            }
                            if (entry.getValue() != "" && entry.getValue() != null) {
                                if (entry.getKey().equals("cpyLinkMode")) {
                                    companyExcelEn.setCpyLinkMode(entry.getValue().toString());
                                }
                            }
                            if (entry.getValue() != "" && entry.getValue() != null) {
                                if (entry.getKey().equals("cpyBindMobile")) {
                                    companyExcelEn.setCpyBindMobile(entry.getValue().toString());
                                }
                            }
                            if (entry.getValue() != "" && entry.getValue() != null) {
                                if (entry.getKey().equals("cpyRemark")) {
                                    companyExcelEn.setCpyRemark(entry.getValue().toString());
                                }
                            }
                            if (entry.getValue() != "" && entry.getValue() != null) {
                                if (entry.getKey().equals("cpyLinkMan")) {
                                    companyExcelEn.setCpyLinkMan(entry.getValue().toString());
                                }
                            }
                            if (entry.getValue() != "" && entry.getValue() != null) {
                                if (entry.getKey().equals("cpyLinkMode")) {
                                    companyExcelEn.setCpyLinkMode(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("cpyPass")) {
                                if (entry.getValue().toString().equals("0")) {
                                    companyExcelEn.setCpyPass("正常");
                                } else if (entry.getValue().toString().equals("1")) {
                                    companyExcelEn.setCpyPass("作古");
                                }
                            }
                            if (entry.getKey().equals("cpyTempLock")) {
                                String s = entry.getValue().toString();
                                if (s.equals("0")) {
                                    companyExcelEn.setCpyTempLock("正常");
                                } else if (s.equals("1")) {
                                    companyExcelEn.setCpyTempLock("冻结");
                                }
                            }
                        }
                        CompanyExcelEns.add(companyExcelEn);
                    }
                    break;
                case "all":
                    //导出全部
                    List<CCompanys> companys = new ArrayList<>();
                    String cpyCo = form.get("cpyCo");
                    String cpyCoClass = form.get("cpyCoClass");
                    String stockCls = form.get("stockCls");
                    QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like(cpyCo != null && cpyCo.equals(""), "CPY_CO", cpyCo);
                    queryWrapper.eq(cpyCoClass != null && cpyCoClass.equals(""), "CPY_CO_CLASS", cpyCoClass);
                    queryWrapper.eq(stockCls != null && stockCls.equals(""), "PDTINF_STOCKCLS", stockCls);
                    queryWrapper.eq("DATA_FROM", plantId);
                    queryWrapper.eq("CPY_DELETED", 0);
                    companys = companysService.list(queryWrapper);

                    for (CCompanys company : companys) {
                        CompanyExcelEn companyExcelEn = CompanyExcelEn.builder().build();
                        companyExcelEn.setCpyCo(company.getCpyCo());
                        companyExcelEn.setCpyShrtName(company.getCpyShrtname());
                        companyExcelEn.setCpyCoClass(company.getCpyCoClass());
                        companyExcelEn.setCpyAddr(company.getCpyAddr());
                        companyExcelEn.setCpyLinkMode(company.getCpyLinkMode());
                        companyExcelEn.setCpyBindMobile(company.getCpyBindMobile());
                        companyExcelEn.setCpyRemark(company.getCpyRemark());
                        if (company.getCpyPass() == 0) {
                            companyExcelEn.setCpyPass("正常");
                        } else if (company.getCpyPass() == 1) {
                            companyExcelEn.setCpyPass("作古");
                        }
                        if (company.getCpyTempLock() == 0) {
                            companyExcelEn.setCpyTempLock("正常");
                        } else if (company.getCpyTempLock() == 1) {
                            companyExcelEn.setCpyTempLock("冻结");
                        }
                        CompanyExcelEns.add(companyExcelEn);
                    }

                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }
                    for (Map<String, Object> newId : newIds) {
                        CompanyExcelEn companyExcelEn = CompanyExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("cpyCo")) {
                                companyExcelEn.setCpyCo(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyShrtName")) {
                                companyExcelEn.setCpyShrtName(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyCoClass")) {
                                companyExcelEn.setCpyCoClass(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyAddr")) {
                                companyExcelEn.setCpyAddr(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyLinkMode")) {
                                companyExcelEn.setCpyLinkMode(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyBindMobile")) {
                                companyExcelEn.setCpyBindMobile(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyRemark")) {
                                companyExcelEn.setCpyRemark(entry.getValue().toString());
                            }
                            if (entry.getKey().equals("cpyPass")) {
                                if (entry.getValue().toString().equals("0")) {
                                    companyExcelEn.setCpyPass("正常");
                                } else if (entry.getValue().toString().equals("1")) {
                                    companyExcelEn.setCpyPass("作古");
                                }
                            }
                            if (entry.getKey().equals("cpyTempLock")) {
                                String s = entry.getValue().toString();
                                if (s.equals("0")) {
                                    companyExcelEn.setCpyTempLock("正常");
                                } else if (s.equals("1")) {
                                    companyExcelEn.setCpyTempLock("冻结");
                                }
                            }
                        }
                        CompanyExcelEns.add(companyExcelEn);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(CompanyExcelEns);
    }

    //拿到充值的厂内收款单位，邹县专供
    public SzResult inPlantCoZx() {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("CPYCLASS_CLASS", "0");
//        .like("CPY_CO","公司");
//        queryWrapper.like("CPY_CO","电厂");
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        List<CCompanyClass> classList = companyClassService.list(queryWrapper);
        List<CCompanys> list = new ArrayList<>();
        if (classList.size() == 0) {
            return SzResult.success(list);
        }
        for (CCompanyClass cCompanyClass : classList) {
            String cpyclassName = cCompanyClass.getCpyclassName();
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO_CLASS", cpyclassName);
            queryWrapper1.eq("CPY_DELETED", 0);
            queryWrapper1.eq("CPY_PASS", 0);
            queryWrapper1.eq("CPY_TEMP_LOCK", 0);
            List<CCompanys> cpyList = companysService.list(queryWrapper1);
            list.addAll(cpyList);
        }
        return SzResult.success(list);
    }
//
    public SzResult inPlantCo() {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("CPYCLASS_CLASS", "0");
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        List<CCompanyClass> classList = companyClassService.list(queryWrapper);
        List<CCompanys> list = new ArrayList<>();
        if (classList.size() == 0) {
            return SzResult.success(list);
        }
        for (CCompanyClass cCompanyClass : classList) {
            String cpyclassName = cCompanyClass.getCpyclassName();
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO_CLASS", cpyclassName);
            queryWrapper1.eq("CPY_DELETED", 0);
            queryWrapper1.eq("CPY_PASS", 0);
            queryWrapper1.eq("CPY_TEMP_LOCK", 0);
            List<CCompanys> cpyList = companysService.list(queryWrapper1);
            list.addAll(cpyList);
        }
        return SzResult.success(list);
    }

    //拿到所有收货单位
    public SzResult receivingCo() {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("CPYCLASS_CLASS", "2");
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        List<CCompanyClass> classList = companyClassService.list(queryWrapper);
        List<CCompanys> list = new ArrayList<>();
        if (classList.isEmpty()) {
            return SzResult.success(list);
        }
        for (CCompanyClass cCompanyClass : classList) {
            String cpyclassName = cCompanyClass.getCpyclassName();
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO_CLASS", cpyclassName);
            queryWrapper1.eq("CPY_DELETED", 0);
            queryWrapper1.eq("CPY_PASS", 0);
//            queryWrapper1.eq("CPY_TEMP_LOCK", 0);
            List<CCompanys> cpyList = companysService.list(queryWrapper1);
            list.addAll(cpyList);
        }
        return SzResult.success(list);
    }

    //拿到所有供货单位
    public SzResult supplyCo() {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("CPYCLASS_CLASS", "1");
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        List<CCompanyClass> classList = companyClassService.list(queryWrapper);
        List<CCompanys> list = new ArrayList<>();
        if (classList.size() == 0) {
            return SzResult.success(list);
        }
        for (CCompanyClass cCompanyClass : classList) {
            String cpyclassName = cCompanyClass.getCpyclassName();
            QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("CPY_CO_CLASS", cpyclassName);
            queryWrapper1.eq("CPY_DELETED", 0);
            queryWrapper1.eq("CPY_PASS", 0);
//            queryWrapper1.eq("CPY_TEMP_LOCK", 0);
            List<CCompanys> cpyList = companysService.list(queryWrapper1);
            list.addAll(cpyList);
        }
        return SzResult.success(list);
    }

    /**
     * 新增单位类型
     *
     * @param cpyClassName
     * @param cpyClassRemark
     * @param cpyClassClass
     * @param token
     * @return
     */
    public SzResult addCoClass(String cpyClassName, String cpyClassRemark, String cpyClassClass, String token,String plantId) throws Exception {
        CCompanyClass cpyClass = new CCompanyClass();
      String uuid = java.util.UUID.randomUUID().toString().replace("-", "");
        if (cpyClassName == null || cpyClassName.equals("")) {
            return SzResult.Error("单位类型不能为空");
        } else {
            //cpyClassName不能为特殊字符
            if (!cpyClassName.matches(".*[\\u4E00-\\u9FA5].*")) {
                return SzResult.Error("单位类型不能为特殊字符");
            }
            cpyClass.setCpyclassName(cpyClassName);
        }
        if (cpyClassRemark != null && !cpyClassRemark.equals("")) {
            cpyClass.setCpyclassRemark(cpyClassRemark);
        }
        if (cpyClassClass == null || cpyClassClass.equals("")) {
            return SzResult.Error("单位类型归属不能为空");
        } else {
            cpyClass.setCpyclassClass(cpyClassClass);
        }
        cpyClass.setCpyclassDeleted(0);

        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CPYCLASS_NAME", cpyClassName);
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        CCompanyClass one = companyClassService.getOne(queryWrapper);
        if (one != null) {
            return SzResult.Error("单位类型已存在");
        }
        QueryWrapper<CCompanyClass> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.orderByDesc("CPYCLASS_ID");
        queryWrapper1.last("LIMIT 1");
        CCompanyClass one1 = companyClassService.getOne(queryWrapper1);
        if (one1 != null) {
            //拿到最后一个id
            BigDecimal cpyclassId = one1.getCpyclassId();
            //id加一
            BigDecimal add = cpyclassId.add(new BigDecimal(1));
            cpyClass.setCpyclassId(add);
        } else {
//            cpyClass.setCpyclassId(uuid);
            cpyClass.setCpyclassId(BigDecimal.valueOf(1));
        }
        //datafrom
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String userId = user.getId();
//        QueryWrapper<SysUserPlantConn> queryWrapper3 = new QueryWrapper<>();
//        queryWrapper3.eq("USER_ID", userId);
//        SysUserPlantConn userPlantConn = userPlantConnService.getOne(queryWrapper3);
//        String plantId = userPlantConn.getPlantId();
        cpyClass.setDataFrom(plantId);
        companyClassService.save(cpyClass);
        return SzResult.Success();
    }

    /**
     * 删除单位类型
     *
     * @param cpyClassId
     * @param token
     * @return
     */
    public SzResult delCoClass(String cpyClassId, String token,String plantId) {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", cpyClassId);
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        CCompanyClass one = companyClassService.getOne(queryWrapper);
        if (one == null) {
            return SzResult.Error("该单位类型不存在");
        }
        String cpyclassName = one.getCpyclassName();
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("CPY_CO_CLASS", cpyclassName);
        queryWrapper1.eq("CPY_DELETED", 0);
        queryWrapper1.eq("CPY_PASS", 0);
        queryWrapper1.eq("CPY_TEMP_LOCK", 0);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.last("LIMIT 1");
        CCompanys one1 = companysService.getOne(queryWrapper1);
        if (one1 != null) {
            return SzResult.Error("有单位归属于此类型，请先解除该单位的此类型绑定。");
        }


        one.setCpyclassDeleted(1);
        one.setDownedID1(0);
        companyClassService.updateById(one);
        return SzResult.Success();
    }

    /**
     * 修改单位类型
     *
     * @param id
     * @param cpyClassName
     * @param cpyClassRemark
     * @param token
     * @return
     */
    public SzResult updateCoClass(String id, String cpyClassName, String cpyClassRemark, String cpyClassClass, String token,String plantId) {

        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);//  5ad18c8f653566e68f3d4886af817849
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        CCompanyClass one = companyClassService.getOne(queryWrapper);
        if (one == null) {
            return SzResult.Error("该单位类型不存在");
        }
        if (cpyClassClass != null && !cpyClassClass.equals("")) {
            one.setCpyclassClass(cpyClassClass);
        }
        if (cpyClassRemark != null) {
            one.setCpyclassRemark(cpyClassRemark);
        }
        one.setDownedID1(0);
        companyClassService.updateById(one);
        return SzResult.Success();

    }

    /**
     * 获取单位类型
     *
     * @param cpyClassId
     * @param cpyClassClass 单位类型归属
     * @param token
     * @return
     */
    public SzResult getCoClass(String cpyClassId, String cpyClassClass, String token,String plantId) {
        QueryWrapper<CCompanyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(cpyClassId != null && !cpyClassId.equals(""), "CPYCLASS_ID", cpyClassId);
        queryWrapper.like(cpyClassClass != null && !cpyClassClass.equals(""), "CPYCLASS_CLASS", cpyClassClass);
        queryWrapper.eq("CPYCLASS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        List<CCompanyClass> one = companyClassService.list(queryWrapper);
        return SzResult.success(one);
    }


    private void coCorrelationTable(String cpyCo, String originalCpyCo, String originalCpyChiCo,String plantId) {
        //对应的c_finace_enter表的CPY_CO字段也要修改
        QueryWrapper<CFinaceEnter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("FINANCE_ENTER_CO", originalCpyCo);
        queryWrapper1.eq("FINANCE_ENTER_CHI_CO", originalCpyChiCo);
        queryWrapper1.eq("DATA_FROM", plantId);
        List<CFinaceEnter> finaceEnterList = finaceEnterService.list(queryWrapper1);
        for (CFinaceEnter cFinaceEnter : finaceEnterList) {
            cFinaceEnter.setFinanceEnterCo(cpyCo);
            cFinaceEnter.setDownedID1(0);

            finaceEnterService.updateById(cFinaceEnter);
        }
        //对应的c_ic_card_info表的CARDINF_CO,CARDINF_CO_SUPPLY,CARDINF_CO_TRAN字段也要修改
        QueryWrapper<CIcCardsInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("CARDINF_CO", originalCpyCo);
        queryWrapper2.eq("CARDINF_CHI_CO", originalCpyChiCo);
        queryWrapper2.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfoList = icCardsInfoService.list(queryWrapper2);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfoList) {
            cIcCardsInfo.setCardinfCo(cpyCo);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);

        }
        QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("CARDINF_CO_SUPPLY", originalCpyCo);
        queryWrapper3.eq("CARDINF_CHICO_SUPPLY", originalCpyChiCo);
        queryWrapper3.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfo1List = icCardsInfoService.list(queryWrapper3);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfo1List) {
            cIcCardsInfo.setCardinfCoSupply(cpyCo);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
        }
        QueryWrapper<CIcCardsInfo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("CARDINF_CO_TRAN", originalCpyCo);
        queryWrapper4.eq("CARDINF_CHICO_TRAN", originalCpyChiCo);
        queryWrapper4.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfo2List = icCardsInfoService.list(queryWrapper4);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfo2List) {
            cIcCardsInfo.setCardinfCoTran(cpyCo);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
        }
        QueryWrapper<COrderDetail> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.eq("ORDETAIL_CO", originalCpyCo);
        queryWrapper5.eq("ORDETAIL_CHICO", originalCpyChiCo);
        queryWrapper5.eq("DATA_FROM", plantId);
        List<COrderDetail> orderDetailList = orderDetailService.list(queryWrapper5);
        for (COrderDetail cOrderDetail : orderDetailList) {
            cOrderDetail.setOrdetailCo(cpyCo);
            cOrderDetail.setDownedID1(0);
            orderDetailService.updateById(cOrderDetail);
        }
        QueryWrapper<CPdtsAuth> queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.eq("PDTSAUTH_CO", originalCpyCo);
        queryWrapper6.eq("PDTSAUTH_CHICO", originalCpyChiCo);
        queryWrapper6.eq("DATA_FROM", plantId);
        List<CPdtsAuth> pdtsAuth = pdtsAuthService.list(queryWrapper6);
        if (pdtsAuth != null) {
            for (CPdtsAuth cPdtsAuth : pdtsAuth) {
                cPdtsAuth.setPdtsauthCo(cpyCo);
                cPdtsAuth.setDownedID1(0);
                pdtsAuthService.updateById(cPdtsAuth);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper7 = new QueryWrapper<>();
        queryWrapper7.eq("WEISHT_CO_SUPPLY", originalCpyCo);
        queryWrapper7.eq("WEISHT_CO_SUPPLY_CHI", originalCpyChiCo);
        queryWrapper7.eq("DATA_FROM", plantId);
        List<CWeightSheets> weightSheets = weightSheetsService.list(queryWrapper7);
        if (weightSheets != null) {
            for (CWeightSheets cWeightSheets : weightSheets) {
                cWeightSheets.setWeishtCoSupply(cpyCo);
                cWeightSheets.setDownedID1(0);
                weightSheetsService.updateById(cWeightSheets);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper8 = new QueryWrapper<>();
        queryWrapper8.eq("WEISHT_CO", originalCpyCo);
        queryWrapper8.eq("WEISHT_CHI_CO", originalCpyChiCo);
        queryWrapper8.eq("DATA_FROM", plantId);
        List<CWeightSheets> weightSheets1 = weightSheetsService.list(queryWrapper8);
        if (weightSheets1 != null) {
            for (CWeightSheets cWeightSheets : weightSheets1) {
                cWeightSheets.setWeishtCo(cpyCo);
                cWeightSheets.setDownedID1(0);
                weightSheetsService.updateById(cWeightSheets);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper9 = new QueryWrapper<>();
        queryWrapper9.eq("WEISHT_CO_TRAN", originalCpyCo);
        queryWrapper9.eq("WEISHT_CHI_CO_TRAN", originalCpyChiCo);
        queryWrapper9.eq("DATA_FROM", plantId);
        List<CWeightSheets> weightSheets2 = weightSheetsService.list(queryWrapper9);
        if (weightSheets2 != null) {
            for (CWeightSheets cWeightSheets : weightSheets2) {
                cWeightSheets.setWeishtCoTran(cpyCo);
                cWeightSheets.setDownedID1(0);
                weightSheetsService.updateById(cWeightSheets);
            }
        }
    }


    private void chicoCorrelationTable(String cpyChico, String originalCpyCo, String originalCpyChiCo,String plantId) {
        //对应的c_finace_enter表的CPY_CHICO字段也要修改
        QueryWrapper<CFinaceEnter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("FINANCE_ENTER_CO", originalCpyCo);
        queryWrapper1.eq("FINANCE_ENTER_CHI_CO", originalCpyChiCo);
        queryWrapper1.eq("DATA_FROM", plantId);
        List<CFinaceEnter> finaceEnter = finaceEnterService.list(queryWrapper1);
        for (CFinaceEnter cFinaceEnter : finaceEnter) {
            cFinaceEnter.setFinanceEnterChiCo(cpyChico);
            cFinaceEnter.setDownedID1(0);
            finaceEnterService.updateById(cFinaceEnter);
        }
        //对应的c_ic_card_info表的CARDINF_CHI_CO,CARDINF_CHICO_SUPPLY,CARDINF_CHICO_TRAN字段也要修改
        QueryWrapper<CIcCardsInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("CARDINF_CO", originalCpyCo);
        queryWrapper2.eq("CARDINF_CHI_CO", originalCpyChiCo);
        queryWrapper2.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfo = icCardsInfoService.list(queryWrapper2);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfo) {
            cIcCardsInfo.setCardinfChiCo(cpyChico);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
        }

        QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("CARDINF_CO_SUPPLY", originalCpyCo);
        queryWrapper3.eq("CARDINF_CHICO_SUPPLY", originalCpyChiCo);
        queryWrapper3.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfo1 = icCardsInfoService.list(queryWrapper3);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfo1) {
            cIcCardsInfo.setCardinfChicoSupply(cpyChico);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
        }
        QueryWrapper<CIcCardsInfo> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("CARDINF_CO_TRAN", originalCpyCo);
        queryWrapper4.eq("CARDINF_CHICO_TRAN", originalCpyChiCo);
        queryWrapper4.eq("DATA_FROM", plantId);
        List<CIcCardsInfo> icCardsInfo2 = icCardsInfoService.list(queryWrapper4);
        for (CIcCardsInfo cIcCardsInfo : icCardsInfo2) {
            cIcCardsInfo.setCardinfChicoTran(cpyChico);
            cIcCardsInfo.setDownedID1(0);
            icCardsInfoService.updateById(cIcCardsInfo);
        }
        QueryWrapper<COrderDetail> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.eq("ORDETAIL_CO", originalCpyCo);
        queryWrapper5.eq("ORDETAIL_CHICO", originalCpyChiCo);
        queryWrapper5.eq("DATA_FROM", plantId);
        List<COrderDetail> orderDetail = orderDetailService.list(queryWrapper5);
        for (COrderDetail cOrderDetail : orderDetail) {
            cOrderDetail.setOrdetailChico(cpyChico);
            cOrderDetail.setDownedID1(0);
            orderDetailService.updateById(cOrderDetail);
        }
        QueryWrapper<CPdtsAuth> queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.eq("PDTSAUTH_CO", originalCpyCo);
        queryWrapper6.eq("PDTSAUTH_CHICO", originalCpyChiCo);
        queryWrapper6.eq("DATA_FROM", plantId);
        List<CPdtsAuth> pdtsAuth = pdtsAuthService.list(queryWrapper6);
        if (pdtsAuth != null) {
            for (CPdtsAuth cPdtsAuth : pdtsAuth) {
                cPdtsAuth.setPdtsauthChico(cpyChico);
                cPdtsAuth.setDownedID1(0);
                pdtsAuthService.updateById(cPdtsAuth);
            }
            QueryWrapper<CWeightSheets> queryWrapper7 = new QueryWrapper<>();
            queryWrapper7.eq("WEISHT_CO_SUPPLY", originalCpyCo);
            queryWrapper7.eq("WEISHT_CO_SUPPLY_CHI", originalCpyChiCo);
            queryWrapper7.eq("DATA_FROM", plantId);
            List<CWeightSheets> weightSheets = weightSheetsService.list(queryWrapper7);
            if (weightSheets != null) {
                for (CWeightSheets cWeightSheets : weightSheets) {
                    cWeightSheets.setWeishtCoSupplyChi(cpyChico);
                    cWeightSheets.setDownedID1(0);
                    weightSheetsService.updateById(cWeightSheets);
                }
            }
            QueryWrapper<CWeightSheets> queryWrapper8 = new QueryWrapper<>();
            queryWrapper8.eq("WEISHT_CO", originalCpyCo);
            queryWrapper8.eq("WEISHT_CHI_CO", originalCpyChiCo);
            queryWrapper8.eq("DATA_FROM", plantId);
            List<CWeightSheets> weightSheets1 = weightSheetsService.list(queryWrapper8);
            if (weightSheets1 != null) {
                for (CWeightSheets cWeightSheets : weightSheets1) {
                    cWeightSheets.setWeishtChiCo(cpyChico);
                    cWeightSheets.setDownedID1(0);
                    weightSheetsService.updateById(cWeightSheets);
                }
            }
            QueryWrapper<CWeightSheets> queryWrapper9 = new QueryWrapper<>();
            queryWrapper9.eq("WEISHT_CO_TRAN", originalCpyCo);
            queryWrapper9.eq("WEISHT_CHI_CO_TRAN", originalCpyChiCo);
            queryWrapper9.eq("DATA_FROM", plantId);
            List<CWeightSheets> weightSheets2 = weightSheetsService.list(queryWrapper9);
            if (weightSheets2 != null) {
                for (CWeightSheets cWeightSheets : weightSheets2) {
                    cWeightSheets.setWeishtChiCoTran(cpyChico);
                    cWeightSheets.setDownedID1(0);
                    weightSheetsService.updateById(cWeightSheets);
                }
            }
        }
    }
//    设置公司是否允许粉煤灰排临时车号

    public SzResult ifOnTemporaryAshTruckQueue(String id,Integer ifOn,String token,String plantId) throws Exception {
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
//CPY_TEMP_ASH_TRUCK
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        CCompanys cCompanys = companysService.getOne(queryWrapper);
        cCompanys.setCpyTempAshTruck(ifOn);
        companysService.updateById(cCompanys);
        if (ifOn ==1 ) {
            sysLogBu.logRecord("单位设置", user.getTrueName(),"开启【"+cCompanys.getCpyCo()+"."+cCompanys.getCpyChico()+"】粉煤灰",plantId);
        }
        return SzResult.Success();
    }

//    设置车辆是否能以特权身份排队
    public SzResult ifOnCpyQueueVip(String id, Integer ifOn, String token, String plantId) throws Exception {
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        CCompanys cCompanys = companysService.getOne(queryWrapper);
        cCompanys.setCpyQueueVip(ifOn);
        companysService.updateById(cCompanys);
        if (ifOn == 1) {
            //            找到他所有正在等待的车辆，将所有等待的车辆的vip状态取消
            QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("TRUCKS_CHECKIN_CO",cCompanys.getId());
            queryWrapper1.eq("TRUCKS_QUEUE_VIP", 0);
            queryWrapper1.eq("TRUCKS_QUEUE_STATUS",1);
            List<CTrucks> trucks = iCTrucksService.list(queryWrapper1);
            for (CTrucks cTrucks : trucks) {
                cTrucks.setTrucksQueueVip(1);
                iCTrucksService.updateById(cTrucks);
            }
            sysLogBu.logRecord("单位设置", user.getTrueName(), "开启【" + cCompanys.getCpyCo() + "." + cCompanys.getCpyChico() + "】特权排队", plantId);
        }
        if (ifOn == 0){
//            找到他所有正在等待的车辆，将所有等待的车辆的vip状态取消
            QueryWrapper<CTrucks> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("TRUCKS_CHECKIN_CO",cCompanys.getId());
            queryWrapper1.eq("TRUCKS_QUEUE_VIP", 1);
            queryWrapper1.eq("TRUCKS_QUEUE_STATUS",1);
            List<CTrucks> trucks = iCTrucksService.list(queryWrapper1);
            for (CTrucks cTrucks : trucks) {
                cTrucks.setTrucksQueueVip(0);
                iCTrucksService.updateById(cTrucks);
            }
            sysLogBu.logRecord("单位设置", user.getTrueName(), "关闭【" + cCompanys.getCpyCo() + "." + cCompanys.getCpyChico() + "】特权排队", plantId);
        }
        return SzResult.Success();
    }
}
